package account.provider;

import java.io.IOException;
import java.util.Properties;

import aimee.common.Logger;
import aimee.common.SQLConnection;

/**
 * 实现更新接口UpdateProvider
 * 
 * @author 毛建杰
 *
 */
public class UpdateProviderImpl implements UpdateProvider
{
	private static Properties properties;
	private static String jdbcDriver;

	static
	{
		try
		{
			properties = new Properties();
			properties.load(RegisterProviderImpl.class.getResourceAsStream("/account.properties"));
			jdbcDriver = "com.mysql.cj.jdbc.Driver";
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public void updatePhone(int userID, String phone)
	{
		String url = properties.getProperty("mysql.passport.url");
		String user = properties.getProperty("mysql.passport.user");
		String password = properties.getProperty("mysql.passport.password");
		try (SQLConnection sqlConnection = new SQLConnection(jdbcDriver, url, user, password))
		{
			int mod = userID % 64;
			String tableName = String.format("user_security_%02d", mod);
			String sql = "update " + tableName + " set phone = '" + phone + "',bind_phone=1 where uid = " + userID + "";
			sqlConnection.executeUpdate(sql);
		}
		catch (Exception e)
		{
			Logger.error(e.getMessage(), e);
		}
	}

	@Override
	public void updateEmail(int userID, String email)
	{
		String url = properties.getProperty("mysql.passport.url");
		String user = properties.getProperty("mysql.passport.user");
		String password = properties.getProperty("mysql.passport.password");
		try (SQLConnection sqlConnection = new SQLConnection(jdbcDriver, url, user, password))
		{
			int mod = userID % 64;
			String tableName = String.format("user_security_%02d", mod);
			String sql = "update " + tableName + " set email = '" + email + "',bind_email=1 where uid = " + userID + "";
			sqlConnection.executeUpdate(sql);
		}
		catch (Exception e)
		{
			Logger.error(e.getMessage(), e);
		}
	}

}
